// $Id: PluginInfo.cs 64 2008-05-06 18:29:47Z nvivo $

using System;
using System.Collections.Generic;
using System.Text;

namespace DBLGen
{
    /// <summary>
    /// Stores information about the plugin and associated classes.
    /// </summary>
    [Serializable]
    public sealed class PluginInfo
    {
        /// <summary>
        /// Creates a PluginInfo from a type.
        /// </summary>
        public PluginInfo(Type sourceType)
        {
            if (sourceType == null)
                throw new ArgumentNullException("pluginType");
            
            // set the type of plugin
            if (sourceType.IsSubclassOf(typeof(BatchPlugin)))
                _baseType = typeof(BatchPlugin);
            else if (sourceType.IsSubclassOf(typeof(ImportPlugin)))
                _baseType = typeof(ImportPlugin);
            else if (sourceType.IsSubclassOf(typeof(ExportPlugin)))
                _baseType = typeof(ExportPlugin);

            // set basic info (name, author, etc)
            PluginAttribute info = (PluginAttribute) Attribute.GetCustomAttribute(sourceType, typeof(PluginAttribute), false);

            if (info != null)
            {
                _name = info.Name;
                _author = info.Author;
                _version = info.Version;
            }
            else
            {
                _name = sourceType.FullName;
            }

            // set plugin type
            _pluginType = sourceType;

            // set settings information
            PluginSettingsAttribute settings = (PluginSettingsAttribute) Attribute.GetCustomAttribute(sourceType, typeof(PluginSettingsAttribute), false);

            if (settings != null)
            {
                _settingsType = Type.GetType(settings.SettingsType);
                _editorType = Type.GetType(settings.EditorType);
            }
        }

        private Type _baseType;
        private string _name;
        private string _author;
        private Version _version;
        private Type _pluginType;
        private Type _settingsType;
        private Type _editorType;

        public Type BaseType
        {
            get
            {
                return _baseType;
            }
        }

        public string Name
        {
            get
            {
                return _name;
            }
        }

        public string Author
        {
            get
            {
                return _author;
            }
        }

        public Version Version
        {
            get
            {
                return _version;
            }
        }

        public Type PluginType
        {
            get
            {
                return _pluginType;
            }
        }

        public Type SettingsType
        {
            get
            {
                return _settingsType;
            }
        }

        public Type EditorType
        {
            get
            {
                return _editorType;
            }
        }

        /// <summary>
        /// Returns a new instance of the plugin.
        /// </summary>
        public Plugin CreatePlugin()
        {
            return (Plugin) Activator.CreateInstance(PluginType);
        }

        /// <summary>
        /// Creates a new instance of the settings class for this plugin.
        /// </summary>
        /// <returns>The new settings or null if the plugin doesn't have a settings class.</returns>
        public PluginSettings CreateSettings()
        {
            if (SettingsType != null)
                return (PluginSettings) Activator.CreateInstance(SettingsType);
            else
                return null;
        }

        /// <summary>
        /// Creates a new instance of the editor for this plugin.
        /// </summary>
        /// <returns>The new editor or null if the plugin doesn't have a settings class.</returns>
        public UI.PluginSettingsEditor CreateEditor()
        {
            if (EditorType != null)
                return (UI.PluginSettingsEditor) Activator.CreateInstance(EditorType);
            else
                return new UI.GenericPluginSettingsEditor();
        }
    }
}
